﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;

namespace DuJianliang.ApplicationPlatform.Common
{
    public class SBODBHelper
    {
        public static DataTable GetOITMInfo(string itemCode)
        {
            string sql = "SELECT ItemCode, ItemName, InvntryUom FROM OITM WHERE ItemCode = @ItemCode";
            try
            {
                Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@ItemCode", DbType.String, itemCode);
                DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];
                return dataTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 获取送修单位
        public static string GetCardName(string missileModel, string missileCode )
        {
            string sql = @"SELECT T2.CardName
                            FROM OSRQ T0
                            JOIN OSRN T1 ON T0.SysNumber=T1.SysNumber AND T0.ItemCode=T1.ItemCode
                            JOIN OCRD T2 ON T2.CardCode=T1.U_Customer
                            WHERE T0.Quantity>0 AND T0.ItemCode=@MissileModel AND T1.DistNumber=@MissileCode";
            try
            {
                Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@MissileModel", DbType.String, missileModel);
                database.AddInParameter(dbCommand, "@MissileCode", DbType.String, missileCode);
                DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    return dataTable.Rows[0]["CardName"].ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}